import * as userInfo from '../common/api/userInfo.js'
import * as config from '../common/config.js'

export function login(callback = ()=>{}) {
	// uni.showLoading({
	// 	title: '登录中...'
	// })
	let openid = ''
	uni.login({
		provider: 'weixin',
		success(loginRes) {
			console.log(loginRes.authResult)
			console.log('loginRes',loginRes)
			uni.request({
				url: `${config.baseUrl}/wxApplet/queryUserOpenId`,
				method: 'GET',
				data: {
					code: loginRes.code,
				},
				success: (res) => {
					// uni.hideLoading()
					console.log('res.data.openid',res);
					openid = res.data.data.openid
					uni.setStorageSync('openid_key', openid)
					console.log('openid',openid);
					callback()
				},
				fail() {
					// uni.hideLoading()
					uni.showToast({
						title: '登录失败!',
						icon: 'error'
					})
				}
			})
		},
		fail() {
			// uni.hideLoading()
			uni.showToast({
				title: '登录失败!',
				icon: 'error'
			})
		}
	});
}


export function checkRegister(){
	login(()=>{
		const openid = uni.getStorageSync('openid_key')
		const param = {
			openId:openid
		}
		userInfo.queryByOpenId(param).then(result => {
			console.log(result.data.data);
		}).catch(error => {
			uni.navigateTo({
				url:'/patientAppPages/pages/login/login'
			})
			console.log(error);
		})
	})
}

export function getPhoneNumber(code,callback = ()=>{}){
	uni.request({
		url:`${config.baseUrl}/wxApplet/queryUserPhoneNum`,
		method:"get",
		data:{
			code:code
		},
		success(e) {
			console.log('success',e);
			console.log('e.data.data.phone_info.purePhoneNumber',e.data.data.phone_info.purePhoneNumber);
			callback(e.data.data)
		},
		fail(e) {
			console.log('fail',e);
			callback(null)
			uni.showToast({
				title:"获取失败",
				icon:"error"
			})
		}
	})
}